{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Most Popular Video Creator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string #sorting #heap-priority-queue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串 #排序 #堆（优先队列）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: mostPopularCreator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最流行的视频创作者"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个字符串数组 <code>creators</code> 和 <code>ids</code> ，和一个整数数组 <code>views</code> ，所有数组的长度都是 <code>n</code> 。平台上第 <code>i</code> 个视频者是&nbsp;<code>creator[i]</code> ，视频分配的 id 是 <code>ids[i]</code> ，且播放量为 <code>views[i]</code> 。</p>\n",
    "\n",
    "<p>视频创作者的 <strong>流行度</strong> 是该创作者的 <strong>所有</strong> 视频的播放量的 <strong>总和</strong> 。请找出流行度 <strong>最高</strong> 创作者以及该创作者播放量 <strong>最大</strong> 的视频的 id 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果存在多个创作者流行度都最高，则需要找出所有符合条件的创作者。</li>\n",
    "\t<li>如果某个创作者存在多个播放量最高的视频，则只需要找出字典序最小的 <code>id</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回一个二维字符串数组<em> </em><code>answer</code><em> </em>，其中<em> </em><code>answer[i] = [creator<sub>i</sub>, id<sub>i</sub>]</code><em> </em>表示<em> </em><code>creator<sub>i</sub></code> 的流行度 <strong>最高</strong> 且其最流行的视频 id 是<em> </em><code>id<sub>i</sub></code><em> </em>，可以按任何顺序返回该结果<em>。</em></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>creators = [\"alice\",\"bob\",\"alice\",\"chris\"], ids = [\"one\",\"two\",\"three\",\"four\"], views = [5,10,5,4]\n",
    "<strong>输出：</strong>[[\"alice\",\"one\"],[\"bob\",\"two\"]]\n",
    "<strong>解释：</strong>\n",
    "alice 的流行度是 5 + 5 = 10 。\n",
    "bob 的流行度是 10 。\n",
    "chris 的流行度是 4 。\n",
    "alice 和 bob 是流行度最高的创作者。\n",
    "bob 播放量最高的视频 id 为 \"two\" 。\n",
    "alice 播放量最高的视频 id 是 \"one\" 和 \"three\" 。由于 \"one\" 的字典序比 \"three\" 更小，所以结果中返回的 id 是 \"one\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>creators = [\"alice\",\"alice\",\"alice\"], ids = [\"a\",\"b\",\"c\"], views = [1,2,2]\n",
    "<strong>输出：</strong>[[\"alice\",\"b\"]]\n",
    "<strong>解释：</strong>\n",
    "id 为 \"b\" 和 \"c\" 的视频都满足播放量最高的条件。\n",
    "由于 \"b\" 的字典序比 \"c\" 更小，所以结果中返回的 id 是 \"b\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == creators.length == ids.length == views.length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= creators[i].length, ids[i].length &lt;= 5</code></li>\n",
    "\t<li><code>creators[i]</code> 和 <code>ids[i]</code> 仅由小写英文字母组成</li>\n",
    "\t<li><code>0 &lt;= views[i] &lt;= 10<sup>5</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [most-popular-video-creator](https://leetcode.cn/problems/most-popular-video-creator/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [most-popular-video-creator](https://leetcode.cn/problems/most-popular-video-creator/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"alice\",\"bob\",\"alice\",\"chris\"]\\n[\"one\",\"two\",\"three\",\"four\"]\\n[5,10,5,4]', '[\"alice\",\"alice\",\"alice\"]\\n[\"a\",\"b\",\"c\"]\\n[1,2,2]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        result, cur = [], 0\n",
    "        for c, g in groupby(sorted(zip(creators, views, ids), key=lambda x: (x[0], -x[1], x[2])), itemgetter(0)):\n",
    "            _, v, i = next(g)\n",
    "            v += sum(x[1] for x in g)\n",
    "            if v >= cur:\n",
    "                v > cur and result.clear()\n",
    "                cur = v\n",
    "                result.append([c, i])\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from sortedcontainers import SortedDict\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        total_view = {}\n",
    "        max_view = 0\n",
    "        for creator, id, view in zip(creators, ids, views):\n",
    "            if creator in total_view:\n",
    "                t = total_view[creator]\n",
    "                t[0] += view\n",
    "                if view > t[1] or (view == t[1] and id < t[2]):\n",
    "                    t[1], t[2] = view, id\n",
    "            else:\n",
    "                total_view[creator] = [view, view, id]\n",
    "            max_view = max(max_view, total_view[creator][0])\n",
    "        return [[creator, id] for creator, (view_sum, _, id) in total_view.items() if view_sum == max_view]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view, id\n",
    "            else: m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                m[name][0]+=view\n",
    "                if view > m[name][1] or view==m[name][1] and id < m[name][2]:\n",
    "                    m[name][1]=view\n",
    "                    m[name][2]=id\n",
    "            else:\n",
    "                m[name]=[view,view,id]\n",
    "            max_view= max(max_view,m[name][0])\n",
    "        return [[name, m[name][2]] for name in m.keys() if m[name][0]==max_view]\n",
    "        \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view, id\n",
    "            else: m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        \n",
    "        my_dict={}\n",
    "        max_view=0\n",
    "        # creator:[total(总流量),最大流量，最大流量的id]\n",
    "        for creator,view,id_ in zip(creators,views,ids):\n",
    "            if creator in my_dict:\n",
    "                cur=my_dict[creator]\n",
    "                cur[0]+=view\n",
    "                if view>cur[1] or (view==cur[1] and id_<cur[2]):\n",
    "                    cur[1],cur[2]=view,id_\n",
    "            else:\n",
    "                my_dict[creator]=[view,view,id_]\n",
    "            max_view=max(max_view,my_dict[creator][0])\n",
    "        ans=[]\n",
    "        # print(my_dict)\n",
    "        # print(max_view)\n",
    "        for creator in my_dict:\n",
    "            if my_dict[creator][0]==max_view:\n",
    "                ans.append([creator,my_dict[creator][2]])\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view , id\n",
    "            else: m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view, id\n",
    "            else: m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        voice = Counter()\n",
    "        ans = 0\n",
    "\n",
    "        for x, y, score in zip(creators, ids, views):\n",
    "            # if x not in voice: voice[x] = [score, y, score]  # 总分 最高播放量视频 \n",
    "            # elif voice[x][0] < score or (voice[x][0] == score and y < voice[x][1]):\n",
    "            #     voice[x] = [score, y, score + voice[x][2]]\n",
    "            # else: voice[x][2] += score\n",
    "\n",
    "            if x in voice:\n",
    "                t = voice[x]\n",
    "                t[2] += score\n",
    "                if t[0] < score or (t[0] == score and y < t[1]):\n",
    "                    t[0], t[1] = score, y\n",
    "            else: voice[x] = [score, y, score]\n",
    "\n",
    "            ans = max(ans, voice[x][2])\n",
    "\n",
    "        return [[i, voice[i][1]] for i in voice if voice[i][2]==ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view, id\n",
    "            else: m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]\n",
    "\n",
    "\n",
    "        # work = defaultdict(list)\n",
    "        # view = defaultdict(int)\n",
    "        # n = len(ids)\n",
    "        # for i in range(n):\n",
    "        #     work[creators[i]].append([ids[i],views[i]])\n",
    "        #     view[creators[i]] += views[i]\n",
    "        \n",
    "        # mx = max(view.values())\n",
    "        # ans = []\n",
    "        # for c,v in view.items():\n",
    "        #     if v == mx:\n",
    "        #         tmp = sorted(work[c], key = lambda x : (-x[1], x[0]))\n",
    "        #         ans.append([c, tmp[0][0]])\n",
    "        # return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        # m  key:name , value: [view_sum,max_view,id]\n",
    "        m, max_view_sum = {},0\n",
    "        for name, id , view in zip(creators,ids,views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view #累加这个人所有视频的播放量\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1],t[2] = view,id\n",
    "                \n",
    "            else:\n",
    "                m[name] = [view,view,id]\n",
    "            max_view_sum = max(max_view_sum,m[name][0])\n",
    "        ans = []\n",
    "        for name ,(view_sum,_,i) in m.items():\n",
    "            if view_sum == max_view_sum:\n",
    "                ans.append([name,i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        # m[name] = [totalview, maxview, id]\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view, id\n",
    "            else:\n",
    "                m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view, id\n",
    "            else: m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "#         m, max_view = {}, 0\n",
    "#         for name, id, view in zip(creators, ids, views):\n",
    "#             if name in m:\n",
    "#                 m[name][0]+=view\n",
    "#                 if view > m[name][1]:\n",
    "#                     m[name][1]=view\n",
    "#                     m[name][2]=id\n",
    "#             else:\n",
    "#                 m[name]=[view,view,id]\n",
    "#             max_view= max(max_view,m[name][0])\n",
    "#         return [[name, m[name][2]] for name in m.keys() if m[name][0]==max_view]\n",
    "        \n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view, id\n",
    "            else: m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view, id\n",
    "            else: m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        cnt = {} # {sm, mx, id}\n",
    "        mx = 0\n",
    "        for c, i, v in zip(creators, ids, views):\n",
    "            if c in cnt:\n",
    "                cnt[c][0] += v\n",
    "                tv, ti = cnt[c][1], cnt[c][2]\n",
    "                if tv < v or tv == v and i < ti:\n",
    "                    tv, ti = v, i\n",
    "                cnt[c][1], cnt[c][2] = tv, ti\n",
    "            else:\n",
    "                cnt[c] = [v, v, i]\n",
    "            mx = max(mx, cnt[c][0])\n",
    "        \n",
    "        return [[a, i] for a, (sv, mxv, i) in cnt.items() if sv == mx]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        voice = Counter()\n",
    "        ans = 0\n",
    "\n",
    "        for i, (x,y) in enumerate(zip(creators, ids)):\n",
    "            if x not in voice:\n",
    "                voice[x] = [views[i], y, views[i]]  # 总分 最高播放量视频 \n",
    "            elif voice[x][0] < views[i] or (voice[x][0] == views[i] and y < voice[x][1]):\n",
    "                voice[x] = [views[i], y, views[i] + voice[x][2]]\n",
    "            else:\n",
    "                voice[x][2] += views[i]\n",
    "\n",
    "            if voice[x][2] > ans:\n",
    "                ans = voice[x][2]\n",
    "\n",
    "        return [[i, voice[i][1]] for i in voice if voice[i][2]==ans]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_num = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or (view == t[1] and id < t[2]):\n",
    "                    t[1] = view\n",
    "                    t[2] = id\n",
    "            else:\n",
    "                m[name] = [view, view, id]\n",
    "            max_view_num = max(max_view_num, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_num]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        \n",
    "        my_dict={}\n",
    "        max_view=0\n",
    "        # creator:[total(总流量),最大流量，最大流量的id]\n",
    "        for creator,view,id_ in zip(creators,views,ids):\n",
    "            if creator in my_dict:\n",
    "                cur=my_dict[creator]\n",
    "                cur[0]+=view\n",
    "                if view>cur[1] or (view==cur[1] and id_<cur[2]):\n",
    "                    cur[1],cur[2]=view,id_\n",
    "            else:\n",
    "                my_dict[creator]=[view,view,id_]\n",
    "            max_view=max(max_view,my_dict[creator][0])\n",
    "        ans=[]\n",
    "        print(my_dict)\n",
    "        print(max_view)\n",
    "        for creator in my_dict:\n",
    "            if my_dict[creator][0]==max_view:\n",
    "                ans.append([creator,my_dict[creator][2]])\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        # {creator:[views_t, ids, views]}\n",
    "        dic,high = {},0\n",
    "        for i in range(len(creators)):\n",
    "            if creators[i] in dic:\n",
    "                dic[creators[i]][0] += views[i]\n",
    "                high = max(high, dic[creators[i]][0])\n",
    "                if views[i] > dic[creators[i]][2]:\n",
    "                    dic[creators[i]][1] = ids[i]\n",
    "                    dic[creators[i]][2] = views[i]\n",
    "                elif views[i] == dic[creators[i]][2]:\n",
    "                    dic[creators[i]][1] = min(dic[creators[i]][1], ids[i])\n",
    "                else:\n",
    "                    pass\n",
    "            else:\n",
    "                dic[creators[i]] = [views[i], ids[i], views[i]]\n",
    "                high = max(high, views[i])\n",
    "        ans = []\n",
    "        for key in dic:\n",
    "            if dic[key][0] == high:\n",
    "                ans.append([key, dic[key][1]])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        voice = Counter()\n",
    "        res = set()\n",
    "        ans = 0\n",
    "\n",
    "        for i, (x,y) in enumerate(zip(creators, ids)):\n",
    "            if x not in voice:\n",
    "                voice[x] = [views[i], y, views[i]]  # 总分 最高播放量视频 \n",
    "            elif voice[x][0] < views[i] or (voice[x][0] == views[i] and y < voice[x][1]):\n",
    "                voice[x] = [views[i], y, views[i] + voice[x][2]]\n",
    "            else:\n",
    "                voice[x][2] += views[i]\n",
    "\n",
    "            if voice[x][2] > ans:\n",
    "                ans = voice[x][2]\n",
    "                res = set()\n",
    "                res.add(x)\n",
    "            elif voice[x][2] == ans: res.add(x)\n",
    "\n",
    "        return [[i, voice[i][1]] for i in res]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m,max_view_sum={},0\n",
    "        for name,id,view in zip(creators,ids,views):\n",
    "            if name in m:\n",
    "                t=m[name]\n",
    "                t[0]+=view\n",
    "                if view>t[1]or view==t[1]and id<t[2]:\n",
    "                    t[1],t[2]=view,id\n",
    "            else: m[name]=[view,view,id]\n",
    "            max_view_sum=max(max_view_sum,m[name][0])\n",
    "        return [[name,id]for name,(view_sum,_,id)in m.items() if view_sum==max_view_sum]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m,max_view_sum={},0\n",
    "        for name, id, view in zip(creators,ids,views):\n",
    "            if name in m:\n",
    "                t=m[name]\n",
    "                t[0]+=view\n",
    "                if view>t[1] or view==t[1] and id <t[2]:\n",
    "                    t[1],t[2]=view,id\n",
    "            else:\n",
    "                m[name]=[view,view,id]\n",
    "            max_view_sum=max(max_view_sum,m[name][0])\n",
    "        return [[name,id] for name,(view_sum,_,id) in m.items() if view_sum==max_view_sum]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        m, max_view_sum = {}, 0\n",
    "        for name, id, view in zip(creators, ids, views):\n",
    "            if name in m:\n",
    "                t = m[name]\n",
    "                t[0] += view\n",
    "                if view > t[1] or view == t[1] and id < t[2]:\n",
    "                    t[1], t[2] = view, id\n",
    "            else: m[name] = [view, view, id]\n",
    "            max_view_sum = max(max_view_sum, m[name][0])\n",
    "        return [[name, id] for name, (view_sum, _, id) in m.items() if view_sum == max_view_sum]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        #哈希表\n",
    "        l1 = {}\n",
    "        x = 0\n",
    "        target = []\n",
    "        for i in range (len(creators)):\n",
    "            if creators[i] not in l1:\n",
    "                l1[creators[i]] = [views[i], views[i], ids[i]]\n",
    "            else:\n",
    "                l1[creators[i]][0] += views[i]\n",
    "                if l1[creators[i]][1] < views[i]:\n",
    "                    l1[creators[i]][1] = views[i]\n",
    "                    l1[creators[i]][2] = ids[i]\n",
    "                elif l1[creators[i]][1] == views[i]:\n",
    "                    p = [ids[i], l1[creators[i]][2]]\n",
    "                    p.sort()\n",
    "                    l1[creators[i]][2] = p[0]\n",
    "        for key in l1:\n",
    "            if l1[key][0] > x:\n",
    "                x = l1[key][0]\n",
    "                target = [ [key, l1[key][2]] ]\n",
    "            elif l1[key][0] == x:\n",
    "                target += [ [key, l1[key][2]] ]\n",
    "        return target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        #哈希表\n",
    "        l1 = {}\n",
    "        x = 0\n",
    "        target = []\n",
    "        for i in range (len(creators)):\n",
    "            if creators[i] not in l1:\n",
    "                l1[creators[i]] = [views[i], views[i], ids[i]]\n",
    "            else:\n",
    "                l1[creators[i]][0] += views[i]\n",
    "                if l1[creators[i]][1] < views[i]:\n",
    "                    l1[creators[i]][1] = views[i]\n",
    "                    l1[creators[i]][2] = ids[i]\n",
    "                elif l1[creators[i]][1] == views[i]:\n",
    "                    p = [ids[i], l1[creators[i]][2]]\n",
    "                    p.sort()\n",
    "                    l1[creators[i]][2] = p[0]\n",
    "        for key in l1:\n",
    "            if l1[key][0] > x:\n",
    "                x = l1[key][0]\n",
    "                target = [ [key, l1[key][2]] ]\n",
    "            elif l1[key][0] == x:\n",
    "                target += [ [key, l1[key][2]] ]\n",
    "        return target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        dict_ = {}\n",
    "        p_dict = {}\n",
    "        for i in range(len(creators)):\n",
    "            key = creators[i]\n",
    "            p_dict[key] = p_dict.get(key, 0) + views[i]\n",
    "            if key not in dict_:\n",
    "                dict_[key] = i\n",
    "            else:\n",
    "                if views[i] > views[dict_[key]]:\n",
    "                    dict_[key] = i\n",
    "                elif views[i] == views[dict_[key]]:\n",
    "                    if ids[i] < ids[dict_[key]]:\n",
    "                        dict_[key] = i\n",
    "        \n",
    "        p_dict = sorted(p_dict.items(), key=lambda x:x[1], reverse=True)\n",
    "        name, p = p_dict[0]\n",
    "        res = [[name, ids[dict_[name]]]]\n",
    "        for name, tmp in p_dict[1::]:\n",
    "            if tmp < p:\n",
    "                break\n",
    "            res.append([name, ids[dict_[name]]])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        #哈希表\n",
    "        l1 = {}\n",
    "        x = 0\n",
    "        target = []\n",
    "        for i in range (len(creators)):\n",
    "            if creators[i] not in l1:\n",
    "                l1[creators[i]] = [views[i], views[i], ids[i]]\n",
    "            else:\n",
    "                l1[creators[i]][0] += views[i]\n",
    "                if l1[creators[i]][1] < views[i]:\n",
    "                    l1[creators[i]][1] = views[i]\n",
    "                    l1[creators[i]][2] = ids[i]\n",
    "                elif l1[creators[i]][1] == views[i]:\n",
    "                    p = [ids[i], l1[creators[i]][2]]\n",
    "                    p.sort()\n",
    "                    l1[creators[i]][2] = p[0]\n",
    "        for key in l1:\n",
    "            if l1[key][0] > x:\n",
    "                x = l1[key][0]\n",
    "                target = [ [key, l1[key][2]] ]\n",
    "            elif l1[key][0] == x:\n",
    "                target += [ [key, l1[key][2]] ]\n",
    "        return target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        n = len(creators)\n",
    "        d = collections.defaultdict(int)\n",
    "        max_view = collections.defaultdict(lambda : -1)\n",
    "        max_id = collections.defaultdict(int)\n",
    "        for i in range(n):\n",
    "            d[creators[i]] += views[i]\n",
    "            if max_view[creators[i]] < views[i]:\n",
    "                max_view[creators[i]] = views[i]\n",
    "                max_id[creators[i]] = ids[i]\n",
    "            elif max_view[creators[i]] == views[i]:\n",
    "                max_id[creators[i]] = min(max_id[creators[i]], ids[i])\n",
    "        \n",
    "        max_val = max(d.values())\n",
    "        # print(d, max_val, max_view)\n",
    "        # print(d, max_val, max_id)\n",
    "        ans = list()\n",
    "        for k, v in d.items():\n",
    "            if v == max_val:\n",
    "                ans.append([k, max_id[k]])\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\r\n",
    "        d = {}\r\n",
    "        for c, i, v in zip(creators, ids, views):\r\n",
    "            if c not in d:\r\n",
    "                d[c] = [0, 0, None]\r\n",
    "            d[c][0] += v\r\n",
    "            if d[c][2] is None or v > d[c][1] or v == d[c][1] and i < d[c][2]:\r\n",
    "                d[c][1] = v\r\n",
    "                d[c][2] = i\r\n",
    "        res = []\r\n",
    "        mxsm = -1\r\n",
    "        for c, (sm, _, mxi) in d.items():\r\n",
    "            if sm > mxsm:\r\n",
    "                res = [[c, mxi]]\r\n",
    "                mxsm = sm\r\n",
    "            elif sm == mxsm:\r\n",
    "                res.append([c, mxi])\r\n",
    "        return res\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        mdict=defaultdict(list)\n",
    "        n=len(ids)\n",
    "        mx=0\n",
    "        for i in range(n):\n",
    "            if creators[i] not in mdict:\n",
    "                mdict[creators[i]].append(views[i])\n",
    "                mdict[creators[i]].append(i)\n",
    "            else:\n",
    "                mdict[creators[i]][0]+=views[i]\n",
    "                mdict[creators[i]].append(views[i])\n",
    "                if views[mdict[creators[i]][1]]<views[i] or views[mdict[creators[i]][1]]==views[i]and ids[i]<ids[mdict[creators[i]][1]]:\n",
    "                    mdict[creators[i]][1]=i\n",
    "            mx=max(mx,mdict[creators[i]][0])\n",
    "        ans=[]\n",
    "        for i in mdict:\n",
    "            if mdict[i][0]==mx:\n",
    "                ans.append([str(i),str(ids[mdict[i][1]])])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        # 哈希表+排序\n",
    "        hashmap=collections.defaultdict(list)\n",
    "        for c,id,v in zip(creators,ids,views):\n",
    "            hashmap[c].append((id,v))\n",
    "        max_total=0\n",
    "        c_list=[]\n",
    "        for c in hashmap:\n",
    "            total=0\n",
    "            for id,v in hashmap[c]:\n",
    "                total+=v\n",
    "            if total>max_total:\n",
    "                c_list=[c]\n",
    "                max_total=total\n",
    "            elif total==max_total:\n",
    "                c_list.append(c)\n",
    "        ans=[]\n",
    "        for c in c_list:\n",
    "            hashmap[c].sort(key=lambda x:(-x[1],x[0]))\n",
    "            ans.append([c,hashmap[c][0][0]])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        total_views = Counter() # 统计所有人的总播放量\n",
    "        best_video = dict()     # 储存一个元组  作者：（视频， 播放量） 表示该作者最高播放视频\n",
    "        for c, i, v in zip(creators, ids, views):\n",
    "            total_views[c] += v\n",
    "            if c not in best_video:\n",
    "                best_video[c] = (i, v)\n",
    "            else:\n",
    "                best_i, best_v = best_video[c]\n",
    "                # 播放量更高或者相同播放，字典序更小\n",
    "                if v > best_v or v == best_v and i < best_i:\n",
    "                    best_video[c] = (i, v)\n",
    "        best_creators = []\n",
    "        mx_views = 0\n",
    "        for k, v in total_views.items():\n",
    "            if v > mx_views:\n",
    "                mx_views = v\n",
    "                best_creators = [[k, best_video[k][0]]]\n",
    "            elif v == mx_views:\n",
    "                best_creators.append([k, best_video[k][0]])\n",
    "        return best_creators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        popMapping = {}\n",
    "        creatorsIds = {}\n",
    "        maxpop = 0\n",
    "        for c, i, v in zip(creators, ids, views):\n",
    "            if(c not in popMapping):\n",
    "                popMapping[c] = 0\n",
    "            popMapping[c] += v\n",
    "            maxpop = max(maxpop, popMapping[c])\n",
    "            if(c not in creatorsIds):\n",
    "                creatorsIds[c] = [i,v]\n",
    "            else:\n",
    "                if(creatorsIds[c][1] < v):\n",
    "                    creatorsIds[c] = [i,v]\n",
    "                elif(creatorsIds[c][1] == v):\n",
    "                    creatorsIds[c][0] = min(i,creatorsIds[c][0])\n",
    "        result = []\n",
    "        for c in popMapping:\n",
    "            if(popMapping[c] == maxpop):\n",
    "                result.append([c, creatorsIds[c][0]])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        popMapping = {}\n",
    "        creatorsIds = {}\n",
    "        maxpop = 0\n",
    "        for c, i, v in zip(creators, ids, views):\n",
    "            if(c not in popMapping):\n",
    "                popMapping[c] = 0\n",
    "            popMapping[c] += v\n",
    "            maxpop = max(maxpop, popMapping[c])\n",
    "            if(c not in creatorsIds):\n",
    "                creatorsIds[c] = [i,v]\n",
    "            else:\n",
    "                if(creatorsIds[c][1] < v):\n",
    "                    creatorsIds[c] = [i,v]\n",
    "                elif(creatorsIds[c][1] == v):\n",
    "                    creatorsIds[c][0] = min(i,creatorsIds[c][0])\n",
    "        result = []\n",
    "        for c in popMapping:\n",
    "            if(popMapping[c] == maxpop):\n",
    "                result.append([c, creatorsIds[c][0]])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution :\n",
    "    def mostPopularCreator(self,creators: [], ids: [], views: []):\n",
    "        counts_dict = {}\n",
    "        max_video_dict = {}\n",
    "        index = 0\n",
    "        for creator in creators:\n",
    "            if creator not in counts_dict:\n",
    "                counts_dict[creator] = views[index]\n",
    "                max_video_dict[creator] = [ids[index],views[index]]\n",
    "            else:\n",
    "                counts_dict[creator] += views[index]\n",
    "                if(views[index]>max_video_dict[creator][1]):\n",
    "                     max_video_dict[creator] = [ids[index],views[index]]\n",
    "                elif(views[index]==max_video_dict[creator][1]):\n",
    "                    max_video_dict[creator][0] = min(max_video_dict[creator][0],ids[index])\n",
    "            index += 1\n",
    "        new_sys1 = sorted(counts_dict.items(), key=lambda d: d[1], reverse=True)\n",
    "        temp = new_sys1[0][1]\n",
    "        ans=[]\n",
    "        for creator,num in new_sys1:\n",
    "            if(num==temp):\n",
    "                ans.append([creator,max_video_dict[creator][0]])\n",
    "            else:\n",
    "                break\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        N = len(creators)\n",
    "        maxPopular = -1\n",
    "        popular = defaultdict(lambda: [-1, [\"\", -1]])\n",
    "        for c, i, v in zip(creators, ids, views):\n",
    "            popular[c][0] += v\n",
    "            if gt(i, v, *popular[c][1]):\n",
    "                popular[c][1][0], popular[c][1][1] = i, v\n",
    "            maxPopular = max(maxPopular, popular[c][0])\n",
    "        \n",
    "        ans = []\n",
    "        for c, p in popular.items():\n",
    "            if p[0] != maxPopular:\n",
    "                continue\n",
    "            ans.append([c, p[1][0]])\n",
    "        return ans\n",
    "\n",
    "\n",
    "def gt(id1: str, v1: int, id2: str, v2: int) -> bool:\n",
    "    if v1 > v2:\n",
    "        return True\n",
    "    elif (v1 == v2) and id1 < id2:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        mapping = {}\n",
    "        maxVal = 0\n",
    "        for i, v1, num in zip(creators, ids, views):\n",
    "            if i not in mapping:\n",
    "                mapping[i] = [[v1, num], num]\n",
    "                maxVal = max(maxVal, num)\n",
    "            else:\n",
    "                if num != mapping[i][0][1]:\n",
    "                    mapping[i][0] = [v1, num] if num > mapping[i][0][1] else mapping[i][0]\n",
    "                    mapping[i][1] += num\n",
    "                    maxVal = max(maxVal, mapping[i][1])\n",
    "                else:\n",
    "\n",
    "                    #mapping[i][0] = [v1, num] if v1 + mapping[i][0][0] < mapping[i][0][0] + v1 else mapping[i][0]\n",
    "                    mapping[i][0] = [v1, num] if mapping[i][0][0] > v1 else mapping[i][0]\n",
    "                    mapping[i][1] += num\n",
    "                    maxVal = max(maxVal, mapping[i][1])\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        returnList = []\n",
    "        for k, v in mapping.items():\n",
    "            if v[1] == maxVal:\n",
    "                tempList = [k, ]\n",
    "                tempList.append(v[0][0])\n",
    "                returnList.append(tempList)\n",
    "        return returnList\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        # 记录每个作者 流行度最高且下标最低的视频\n",
    "        # 记录每个作者最高的流行度\n",
    "\n",
    "        voice = Counter()\n",
    "        popular = Counter()\n",
    "\n",
    "        for i, (x,y) in enumerate(zip(creators, ids)):\n",
    "            if x not in voice:\n",
    "                voice[x] = [y, views[i]]\n",
    "            elif views[i] > voice[x][1] or (views[i] == voice[x][1] and y < voice[x][0]):\n",
    "                voice[x] = [y, views[i]]\n",
    "\n",
    "            popular[x] += views[i]\n",
    "\n",
    "        s = sorted(popular.items(), key=lambda z: -z[1])\n",
    "        ans = [[s[0][0], voice[s[0][0]][0]]]\n",
    "        # print(ans, s)\n",
    "        i = 0\n",
    "        while i+1 < len(s) and s[i+1][1] == s[i][1]:\n",
    "            x,y = s[i+1]\n",
    "            ans += [[x, voice[x][0]]]\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        creatorMap = defaultdict(lambda: [0, [0, \"zzzzz\"]])\n",
    "        for who, _id, view in zip(creators, ids, views):\n",
    "            info = creatorMap[who]\n",
    "            info[0] += view\n",
    "            info[1] = min(info[1], [-view, _id])\n",
    "        maxTotal = 0\n",
    "        res = []\n",
    "        for who, (total, _max) in creatorMap.items():\n",
    "            if total < maxTotal: continue\n",
    "            if total > maxTotal:\n",
    "                maxTotal = total\n",
    "                res.clear()\n",
    "            res.append([who, _max[1]])\n",
    "        return res\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        creator_video = {}\n",
    "        creator_view = {}\n",
    "        for i in range(len(creators)):\n",
    "            creator = creators[i]\n",
    "            video = ids[i]\n",
    "            view = views[i]\n",
    "            if creator not in creator_video:\n",
    "                creator_video[creator] = []\n",
    "            creator_video[creator].append((video, view))\n",
    "            if creator not in creator_view:\n",
    "                creator_view[creator] = 0\n",
    "            creator_view[creator] += view\n",
    "\n",
    "    # 找到最高流行度的所有创作者\n",
    "        max_popularity = max(creator_view.values())\n",
    "        popular_creators = [k for k, v in creator_view.items() if v == max_popularity]\n",
    "\n",
    "    # 找到最高播放量的视频id\n",
    "        answer = []\n",
    "        for creator in popular_creators:\n",
    "            videos = creator_video[creator]\n",
    "            videos.sort()\n",
    "            max_views = max(v for _, v in videos)\n",
    "            for video, view in videos:\n",
    "                if view == max_views:\n",
    "                    answer.append([creator, video])\n",
    "                    break\n",
    "\n",
    "        return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        creator_video = {}\n",
    "        creator_view = {}\n",
    "        for i in range(len(creators)):\n",
    "            creator=creators[i]\n",
    "            view=views[i]\n",
    "            video=ids[i]\n",
    "            if creator not in creator_video:\n",
    "                creator_video[creator]=[]\n",
    "            creator_video[creator].append((video,view))\n",
    "            if creator not in creator_view:\n",
    "                creator_view[creator]=0\n",
    "            creator_view[creator]+=view\n",
    "        max_pop=max(creator_view.values())\n",
    "        pop_creators=[k for k,v in creator_view.items() if v==max_pop] \n",
    "        \n",
    "        ans=[]\n",
    "        for creator in pop_creators:\n",
    "            videos=creator_video[creator]\n",
    "            videos.sort()\n",
    "            max_views=max(u for _,u in videos)\n",
    "            for video,view in videos:\n",
    "                if view == max_views:\n",
    "                    ans.append([creator,video])\n",
    "                    break\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        creator_video = {}\n",
    "        creator_view = {}\n",
    "        for i in range(len(creators)):\n",
    "            creator=creators[i]\n",
    "            view=views[i]\n",
    "            video=ids[i]\n",
    "            if creator not in creator_video:\n",
    "                creator_video[creator]=[]\n",
    "            creator_video[creator].append((video,view))\n",
    "            if creator not in creator_view:\n",
    "                creator_view[creator]=0\n",
    "            creator_view[creator]+=view\n",
    "        max_pop=max(creator_view.values())\n",
    "        pop_creators=[k for k,v in creator_view.items() if v==max_pop] \n",
    "        \n",
    "        ans=[]\n",
    "        for creator in pop_creators:\n",
    "            videos=creator_video[creator]\n",
    "            videos.sort()\n",
    "            max_views=max(u for _,u in videos)\n",
    "            for video,view in videos:\n",
    "                if view == max_views:\n",
    "                    ans.append([creator,video])\n",
    "                    break\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        creator_video = {}\n",
    "        creator_view = {}\n",
    "        for i in range(len(creators)):\n",
    "            creator=creators[i]\n",
    "            view=views[i]\n",
    "            video=ids[i]\n",
    "            if creator not in creator_video:\n",
    "                creator_video[creator]=[]\n",
    "            creator_video[creator].append((video,view))\n",
    "            if creator not in creator_view:\n",
    "                creator_view[creator]=0\n",
    "            creator_view[creator]+=view\n",
    "        max_pop=max(creator_view.values())\n",
    "        pop_creators=[k for k,v in creator_view.items() if v==max_pop] \n",
    "        \n",
    "        ans=[]\n",
    "        for creator in pop_creators:\n",
    "            videos=creator_video[creator]\n",
    "            videos.sort()\n",
    "            max_views=max(u for _,u in videos)\n",
    "            for video,view in videos:\n",
    "                if view == max_views:\n",
    "                    ans.append([creator,video])\n",
    "                    break\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        creator_video = {}\n",
    "        creator_view = {}\n",
    "        for i in range(len(creators)):\n",
    "            creator=creators[i]\n",
    "            view=views[i]\n",
    "            video=ids[i]\n",
    "            if creator not in creator_video:\n",
    "                creator_video[creator]=[]\n",
    "            creator_video[creator].append((video,view))\n",
    "            if creator not in creator_view:\n",
    "                creator_view[creator]=0\n",
    "            creator_view[creator]+=view\n",
    "        max_pop=max(creator_view.values())\n",
    "        pop_creators=[k for k,v in creator_view.items() if v==max_pop] \n",
    "        \n",
    "        ans=[]\n",
    "        for creator in pop_creators:\n",
    "            videos=creator_video[creator]\n",
    "            videos.sort()\n",
    "            max_views=max(u for _,u in videos)\n",
    "            for video,view in videos:\n",
    "                if view == max_views:\n",
    "                    ans.append([creator,video])\n",
    "                    break\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        r = {}\n",
    "        m = 0\n",
    "        for c, i, v in zip(creators, ids, views):\n",
    "            t = r.get(c, [0, []])\n",
    "            t[0] += v\n",
    "            heapq.heappush(t[1], (-v, i))\n",
    "            r[c] = t\n",
    "            m = max(m, t[0])\n",
    "        ret = []\n",
    "        for k, v in r.items():\n",
    "            if m == v[0]:\n",
    "                ret.append([k, heapq.heappop(v[1])[1]])\n",
    "        return ret\n",
    "        # print(f\"r {r}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        dicts = {}\n",
    "        max_views=0\n",
    "        for i,c in enumerate(creators):\n",
    "            if c not in dicts:\n",
    "                dicts[c]={'sums':views[i],'maxId':ids[i],'maxNum':views[i]}\n",
    "            else:\n",
    "                dicts[c]['sums']+=views[i]\n",
    "                if views[i]>dicts[c]['maxNum']:\n",
    "                    dicts[c]['maxId']=ids[i]\n",
    "                    dicts[c]['maxNum']=views[i]\n",
    "                elif views[i]==dicts[c]['maxNum'] and ids[i]<dicts[c]['maxId']:\n",
    "                    dicts[c]['maxId']=ids[i]\n",
    "                    dicts[c]['maxNum']=views[i]\n",
    "                    \n",
    "            if dicts[c]['sums']>max_views:\n",
    "                max_views=dicts[c]['sums']\n",
    "        result = []\n",
    "        for k in dicts.keys():\n",
    "            if dicts[k][\"sums\"]==max_views:\n",
    "                result.append([k,dicts[k][\"maxId\"]])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        work = defaultdict(list)\n",
    "        view = defaultdict(int)\n",
    "        n = len(ids)\n",
    "        for i in range(n):\n",
    "            work[creators[i]].append([ids[i],views[i]])\n",
    "            view[creators[i]] += views[i]\n",
    "        \n",
    "        mx = max(view.values())\n",
    "        ans = []\n",
    "        for c,v in view.items():\n",
    "            if v == mx:\n",
    "                tmp = sorted(work[c], key = lambda x : (-x[1], x[0]))\n",
    "                ans.append([c, tmp[0][0]])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        d_view = defaultdict(int)\n",
    "        d_pop = defaultdict(list)\n",
    "        for c, i, v in zip(creators, ids, views):\n",
    "            d_view[c] += v\n",
    "            d_pop[c].append((v, i))\n",
    "\n",
    "        maxView = max(d_view.values())\n",
    "        res = []\n",
    "        for c in d_view:\n",
    "            if d_view[c] == maxView:\n",
    "                res.append([c, sorted(d_pop[c], key = lambda x: (-x[0], x[1]))[0][1]])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        d_view = defaultdict(int)\n",
    "        d_pop = defaultdict(list)\n",
    "        for c, i, v in zip(creators, ids, views):\n",
    "            d_view[c] += v\n",
    "            d_pop[c].append((v, i))\n",
    "\n",
    "        maxView = max(d_view.values())\n",
    "        res = []\n",
    "        for c in d_view:\n",
    "            if d_view[c] == maxView:\n",
    "                res.append([c, sorted(d_pop[c], key = lambda x: (-x[0], x[1]))[0][1]])\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "      ans = []\n",
    "      userScore = defaultdict(int)\n",
    "      userVideo = defaultdict(list)\n",
    "      for user, id, view in zip(creators, ids, views):\n",
    "        userScore[user] += view\n",
    "        userVideo[user].append((id, view))\n",
    "      mx_score = max(userScore.values())\n",
    "      for user in userScore:\n",
    "        if userScore[user] == mx_score:\n",
    "          bestVideo = min(userVideo[user], key=lambda x: (-x[1], x[0]))\n",
    "          ans.append([user, bestVideo[0]])\n",
    "      return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        name_cnt = collections.defaultdict(int)\n",
    "        name_vi = collections.defaultdict(list)\n",
    "\n",
    "        maxF = 0\n",
    "        for name, ID, view in zip(creators, ids, views):\n",
    "            name_cnt[name] += view\n",
    "            name_vi[name].append((-view, ID))\n",
    "            \n",
    "            maxF = max(maxF, name_cnt[name])\n",
    "        \n",
    "        res = []\n",
    "        for name, cnt in name_cnt.items():\n",
    "            if cnt == maxF:\n",
    "                ID = sorted(name_vi[name])[0][1]\n",
    "                res.append([name, ID])\n",
    "        return res\n",
    "                \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        N = len(creators)\n",
    "        maxPopular = -1\n",
    "        popular = defaultdict(lambda: [-1, Video(\"\", -1)])\n",
    "        for i in range(N):\n",
    "            c = creators[i]\n",
    "            popular[c][0] += views[i]\n",
    "            v = Video(ids[i], views[i])\n",
    "            if v > popular[c][1]:\n",
    "                popular[c][1] = v\n",
    "            maxPopular = max(maxPopular, popular[c][0])\n",
    "        \n",
    "        ans = []\n",
    "        for c, p in popular.items():\n",
    "            if p[0] != maxPopular:\n",
    "                continue\n",
    "            ans.append([c, p[1].id])\n",
    "        return ans\n",
    "\n",
    "\n",
    "class Video:\n",
    "    def __init__(self, id: str, view: int):\n",
    "        self.id = id\n",
    "        self.view = view\n",
    "    \n",
    "    def __eq__(self, other):\n",
    "        return (self.id == other.id) and (self.view == other.view)\n",
    "    \n",
    "    def __gt__(self, other):\n",
    "        if self.view > other.view:\n",
    "            return True\n",
    "        elif (self.view == other.view) and self.id < other.id:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import *\n",
    "from heapq import *\n",
    "from sys import *\n",
    "from math import *\n",
    "from itertools import *\n",
    "from functools import *\n",
    "from string import *\n",
    "from bisect import *\n",
    "from graphlib import *\n",
    "from sortedcontainers import *\n",
    "from copy import *\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        cnts = Counter()\n",
    "        for c, id, v in zip(creators, ids, views):\n",
    "            cnts[c] += v\n",
    "        max_val, max_cnt = cnts.most_common(1)[0]\n",
    "        g = defaultdict(list)\n",
    "        for c, id, v in zip(creators, ids, views):\n",
    "            if cnts[c] == max_cnt: g[c].append((-v, id))\n",
    "        ans = [[k, sorted(v)[0][1]] for k, v in g.items()]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        N = len(creators)\n",
    "        popular = defaultdict(int)\n",
    "        maxViews = defaultdict(lambda: Video(\"\", -1))\n",
    "        for i in range(N):\n",
    "            c = creators[i]\n",
    "            v = Video(ids[i], views[i])\n",
    "            if v > maxViews[c]:\n",
    "                maxViews[c] = v\n",
    "            popular[c] += views[i]\n",
    "        \n",
    "        x = sorted(popular.items(), key=lambda x: (x[1], x[0]), reverse=True)\n",
    "        ans = [[x[0][0], maxViews[x[0][0]].id]]\n",
    "        for i in range(1, len(x)):\n",
    "            if x[i][1] != x[i-1][1]:\n",
    "                break\n",
    "            ans.append([x[i][0], maxViews[x[i][0]].id])\n",
    "        return ans\n",
    "\n",
    "\n",
    "class Video:\n",
    "    def __init__(self, id: str, view: int):\n",
    "        self.id = id\n",
    "        self.view = view\n",
    "    \n",
    "    def __eq__(self, other):\n",
    "        return (self.id == other.id) and (self.view == other.view)\n",
    "    \n",
    "    def __gt__(self, other):\n",
    "        if self.view > other.view:\n",
    "            return True\n",
    "        elif (self.view == other.view) and self.id < other.id:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        d = defaultdict(dict)\n",
    "        n = len(creators)\n",
    "        counter = Counter()\n",
    "        maxviews = 0\n",
    "        for i in range(n):\n",
    "            d[creators[i]][ids[i]] = views[i]\n",
    "            counter[creators[i]] += views[i]\n",
    "            maxviews = max(maxviews, counter[creators[i]])\n",
    "            \n",
    "        ret = []\n",
    "        for k, v in d.items():\n",
    "            if counter[k] == maxviews:\n",
    "                mv = -1\n",
    "                song = ''\n",
    "                for k2, v2 in v.items():\n",
    "                    if mv < v2 or (mv == v2 and song > k2):\n",
    "                        song = k2\n",
    "                        mv = v2\n",
    "                ret.append([k, song])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        from collections import defaultdict\n",
    "        id2cre = defaultdict(str)\n",
    "        cre2id = defaultdict(list)\n",
    "        id2cnt = defaultdict(int)\n",
    "        cre2vi = defaultdict(int)\n",
    "        \n",
    "        n = len(creators)\n",
    "       \n",
    "        for i in range(n) :\n",
    "            id2cre[ids[i]] = creators[i]\n",
    "            cre2id[creators[i]].append([ids[i],views[i]])\n",
    "\n",
    "        for cr,id2view in cre2id.items() :\n",
    "            for id ,vi in id2view :\n",
    "                cre2vi[cr] += vi\n",
    "        for id2view in cre2id.values() :\n",
    "            for id,vi in id2view :\n",
    "                id2cnt[id] += vi\n",
    "        \n",
    "        #print(cre2vi) \n",
    "        ans = []\n",
    "        maxCrV = max(cre2vi.values())\n",
    "        for cr,vi in cre2vi.items() :\n",
    "            if vi == maxCrV :\n",
    "                ans.append(cr)\n",
    "     \n",
    "        res = []\n",
    "        for cr in ans :\n",
    "            maxV = -1\n",
    "            preId = 'None'\n",
    "            for  id,cnt in cre2id[cr]:\n",
    "                \n",
    "                if cnt > maxV or (cnt == maxV and id < preId ) :\n",
    "                    preId = id\n",
    "                    maxV = cnt\n",
    "\n",
    "                \n",
    "            res.append([cr,preId])\n",
    "        return (res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        n = len(creators)\n",
    "        dct = defaultdict(lambda: defaultdict(int))\n",
    "        cnt = defaultdict(int)\n",
    "        for i in range(n):\n",
    "            cnt[creators[i]] += views[i]\n",
    "            dct[creators[i]][ids[i]] += views[i]\n",
    "        ceil = max(cnt.values())\n",
    "        ans = []\n",
    "        for name in cnt:\n",
    "            if cnt[name] == ceil:\n",
    "                x = 0\n",
    "                for ls in dct[name]:\n",
    "                    if dct[name][ls] > x:\n",
    "                        x= dct[name][ls]\n",
    "                lst = [ls for ls in dct[name] if dct[name][ls] == x]\n",
    "                ans.append([name, min(lst)])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]:\n",
    "        info = {}\n",
    "        for creator, id, view in zip(creators, ids, views):\n",
    "            if creator not in info:\n",
    "                info[creator] = [[], 0]\n",
    "            info[creator][0].append([id, view])\n",
    "            info[creator][1] += view\n",
    "        info_list = [[k, v] for k, v in info.items()]\n",
    "        info_list.sort(key=lambda x: x[1][1], reverse=True)\n",
    "        \n",
    "\n",
    "        for i in range(len(info_list)):\n",
    "            info_list[i][1][0].sort(key=lambda x: (-x[1], x[0]))\n",
    "        \n",
    "        ans = [[info_list[0][0], info_list[0][1][0][0][0]]]\n",
    "\n",
    "        max_val = info_list[0][1][1]\n",
    "        for i in range(1, len(info_list)):\n",
    "            this_val = info_list[i][1][1]\n",
    "            if this_val == max_val:\n",
    "                ans.append([info_list[i][0], info_list[i][1][0][0][0]])\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
